<?php

namespace App\Console\Commands\OpenShop;

use App\Exceptions\Custom\ResponseHttpException;
use App\Library\FeiShu\SendFs;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Remote\Rms\RmsShopRemote;
use App\Service\OpenShop\SiteLocation\GuanyuanDataService;
use Illuminate\Console\Command;

class OpenShopShopBusinessNotice extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'open_shop:shop_business_notice';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '查询guanyuan的营业数据,超过15天没有记录的店铺发飞书提醒';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $date      = date('Y-m-d');
        $shopLists = RmsShopRemote::getFormalUseShopLists($date);
        $shopLists = $shopLists['shopLists'] ?? [];
        if (!$shopLists) {
            return 0;
        }

        $noOrderShopArray = []; //超过15天没有订单记录的店铺

        collect($shopLists)->chunk(100)->each(
            function ($shopLists) use ($date, &$noOrderShopArray) {
                foreach ($shopLists as $k => $shop) {

                    // 过滤测试门店数据
                    if ($shop['shopCode'] >= OpenShopSignOrderModel::SHOP_CODE_TEST || strpos($shop['name'], '测试') !== false) {
                        unset($shopLists[$k]);
                    }
                }
                $shopCodes        = array_column($shopLists->toArray(), 'shopCode');

                try {
                    GuanyuanDataService::setTimeOut(10);
                    $guanyuanDataList = GuanyuanDataService::getShopBusinessInfoByCode($shopCodes, 16);
                } catch (\Exception $exception) {
                    throw new ResponseHttpException(504, null, '获取门店详情超时,请稍后再试');
                }

                $shopLists = collect($shopLists)->keyBy('shopCode')->toArray();
                foreach ($guanyuanDataList as $guanyuanData) {
                    if ($guanyuanData['orderNum'] > 0 && $guanyuanData['date'] != $date) {
                        if (!isset($shopLists[$guanyuanData['code']]['day'])) {
                            $shopLists[$guanyuanData['code']]['day'] = 1;
                        } else {
                            $shopLists[$guanyuanData['code']]['day'] += 1;
                        }
                    }
                }

                foreach ($shopLists as $shop) {
                    if (!(isset($shop['day']) && $shop['day'] > 0)) {
                        $noOrderShopArray[] = $shop;
                    }
                }
            }
        );

        $content = '';
        if ($noOrderShopArray) {
            foreach ($noOrderShopArray as $shop) {
                $content .= $shop['shopCode'] . $shop['name'] . PHP_EOL;
            }

            SendFs::newRobot(SendFs::ROBOT_OPEN_SHOP)
                ->setTitleContent("超过15天没有订单记录的店铺执行结果", $content)
                ->setAtUid('e119e19f')// 扶苏
                ->send();

        }
    }


}
